﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BattleFury.Lib.Controls
{
	public class Skin
	{
		private List<string> _cachedPartNames;

		public string Name { get; set; }
		public string AssetName { get; set; }
		public List<Part> Parts { get; set; }

		[ContentSerializerIgnore]
		public Texture2D Texture { get; set; }

		public Part this[string partName]
		{
			get { return GetPart(partName); }
		}

		public Skin()
		{
			Parts = new List<Part>();
			_cachedPartNames = new List<string>();
		}

		public void LoadTexture()
		{
			if (string.IsNullOrEmpty(AssetName))
				throw new Exception("No Asset Name set to load Texture from.");

			Texture = RenderEngine.Get().Content.Load<Texture2D>(AssetName);
		}

		public void CachePartNames()
		{
			_cachedPartNames.Clear();
			foreach (var part in Parts)
			{
				_cachedPartNames.Add(part.Name);
			}
		}

		public Part GetPart(string name)
		{
			var index = _cachedPartNames.IndexOf(name);
			return Parts[index];
		}
	}
}